var form, $,areaData;
layui.config({
    base : "/js/"
}).extend({
    "address" : "address"
});
layui.use(['form','layer','upload','laydate',"address"],function(){
    form = layui.form;
    $ = layui.jquery;
    var layer = parent.layer === undefined ? layui.layer : top.layer;


    //回显用户个人信息
    var _ticket = $.cookie("token");


    if (_ticket != null && _ticket !== "") {
        var userT = JSON.parse(_ticket);
        //确认ip
        $.ajax({
            url: "/admin/user/getIp",//单点登录链接
            type: "GET",
            success: function (data) {

                if (data != userT.loginIp) {
                    layer.confirm('用户登录已过期，请中重新登录', {icon: 3, title: '提示信息'}, function (tips) {
                    });
                    window.location.href = "/login";
                }
                else{
                    $.ajax({
                        url: '/admin/user/' + userT.id,
                        type: 'GET',
                        success: function (user) {
                            $("#id").val(user.id);
                            $("#loginName").val(user.loginName);
                        }
                    });

                // $.ajax({
                //     url: '/admin/user/' + userT.id,
                //     type: 'GET',
                //     success: function (user) {
                //         $("#id").val(user.id);
                //         $("#loginName").val(user.loginName);
                //
                //     }
                // })
                }
            }
        });


        //修改密码
        form.on("submit(changePwd)",function(data){
            var index = layer.msg('提交中，请稍候',{icon: 16,time:false,shade:0.8});
            $.ajax({
                url: '/admin/user/' + userT.id,
                type: 'GET',
                success: function (user) {
                    if(user.password==data.field.oldPassword){

                        if(data.field.repass!=data.field.password)
                        {
                            layer.msg('两次密码不同，注册失败！');
                        }
                        else {
                            $.ajax({
                                url: '/admin/user/edit',
                                type: "POST",
                                data: {
                                    id: data.field.id,
                                    loginName: data.field.loginName,
                                    password: data.field.password,
                                    roleId:userT.roleId
                                },
                                success: function (data) {
                                    if (data.code == 200) {
                                        layer.close(index);
                                        layer.msg("修改成功！");
                                        location.reload();
                                    } else {
                                        layer.close(index);
                                        layer.msg("修改失败！请重试");
                                        location.reload();
                                    }
                                }
                            })
                        }
                    } else{
                        layer.msg(' 原始密码不正确！');
                    }
                }


            })


            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        })




    } else {
        layer.confirm('用户登录已过期，请中重新登录', {icon: 3, title: '提示信息'}, function (tips) {
        });
        window.location.href = "/login";
    }



});